package com.cy.game

import android.content.Context
import android.view.View
import android.widget.TextView
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.base.BaseFragment
import com.helloandroid.tools.MyTimeUtils

class CyHomeFragment : BaseFragment() {

    lateinit var cyAct: CyHomeActivity

    lateinit var tvCoin: TextView
    lateinit var tvPower: TextView
    lateinit var tvTime: TextView

    // 关卡文字
    lateinit var tvQian: TextView
    lateinit var tvBai: TextView
    lateinit var tvShi: TextView
    lateinit var tvGe: TextView

    // 是否初始化完毕
    var inited = false

    override fun onAttach(context: Context) {
        super.onAttach(context)
        cyAct = context as CyHomeActivity
    }

    override fun getLayoutResId(): Int = R.layout.fragment_cy_home

    override fun initView(view: View) {
        // init views
        tvCoin = findViewById(R.id.home_coinText)
        tvPower = findViewById(R.id.home_power_countText)
        tvTime = findViewById(R.id.home_power_timeText)

        // 千 百  十  个
        tvQian = findViewById(R.id.levelText_0)
        tvBai = findViewById(R.id.levelText_1)
        tvShi = findViewById(R.id.levelText_2)
        tvGe = findViewById(R.id.levelText_3)

        // back
        findBtn(R.id.btnBack).setOnClickListener {
            cyAct.finish()
        }

        // btn_play
        findBtn(R.id.btnPlay).setOnClickListener { btnPlayClicked(); }

        // 添加体力
        findBtn(R.id.btnAddPower).setOnClickListener { btnAddPowerClicked(); }
    }

    override fun initData() {
        if (cyAct.isReady) {
            initUI()
        }
        inited = true
    }

    fun initUI() {
        val info = cyAct.mInfo!!
        //
        val power = info.power
        val cur = AppUtil.sysTime
        val last = if (info.lastRepowerMs == 0L) { cur } else { info.lastRepowerMs }

        // 更新体力
        if (power < 20) {
            // 不进行添加
            var newAdded: Int = ((cur - last) / CyHomeActivity.TenMinutes).toInt()
            var passedMsPerTurn = (cur - last) % CyHomeActivity.TenMinutes
            if (newAdded + power > 20) {
                newAdded = 20 - power
                cyAct.countDownTimeMs = 0L
            } else {
                cyAct.countDownTimeMs = passedMsPerTurn
            }

            if (newAdded >= 1) {
                // 发送一个增加体力的请求
                AppUtil.appViewModel().cyRepower(cur, newAdded, false)
                info.increasePower(newAdded)
                info.lastRepowerMs = cur
            }
        }

        tvCoin.text = "${AppUtil.user.coin}"
        tvPower.text = "${info.power}"

        val qian = info.curLevel / 1000
        val bai = (info.curLevel - qian * 1000) / 100
        val shi = (info.curLevel - qian * 1000 - bai * 100) / 10
        val ge = info.curLevel % 10

        tvQian.text = "$qian"
        tvBai.text = "$bai"
        tvShi.text = "$shi"
        tvGe.text = "$ge"
    }

    private fun btnAddPowerClicked() {
        if (!cyAct.isReady) {
            AppUtil.toast("数据还未加载好!")
            return
        }

        // 显示对话框
        AddPowerDialog(cyAct).show()
    }

    private fun btnPlayClicked() {
        if (!cyAct.isReady) {
            AppUtil.toast("数据还未加载好!")
            return
        }

        val info = cyAct.mInfo!!

        // 判断是否全部通关
        if (info.curLevel > info.totalLevel) {
            AppUtil.toast("已经通关所有关卡！后续关卡敬请期待！")
            return
        }

        cyAct.realStartGame(info)
    }


    fun tick() {
        val info = cyAct.mInfo!!
        tvCoin.text = "${AppUtil.user.coin}"
        tvPower.text = "${info.power}"
        if (info.power >= 20) {
            tvTime.text = "已满"
        } else {
            tvTime.text = MyTimeUtils.format_mmss(CyHomeActivity.TenMinutes - cyAct.countDownTimeMs)
        }
    }
}